package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import com.amap.api.maps.AMap
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.WeatherInfoResponse

/**
 * @Author      : hxw
 * @Date        :2023/2/8 11:18
 * @Describe    :
 */
class MapInfoWinAdapter(context: Context, data: WeatherInfoResponse) : AMap.InfoWindowAdapter {

    private var mContext: Context? = null
    private var latLng: LatLng? = null
    private lateinit var tvWeather: AppCompatTextView
    private lateinit var tvTemperature: AppCompatTextView
    private lateinit var tvWindDirectionPower: AppCompatTextView
    private lateinit var tvWeatherInfoFlyTime: AppCompatTextView
    private lateinit var tvWeatherInfoFlyPlace: AppCompatTextView
    private lateinit var tvWeatherInfoDistance: AppCompatTextView
    private var mSnippet: String = ""
    private var mTitle: String = ""
    private var mData: WeatherInfoResponse

    override fun getInfoWindow(p0: Marker?): View {
        p0?.let { initData(it) }
        return initView()
    }

    private fun initData(marker: Marker) {
        //当前点位经纬度
        latLng = marker.position
        //当前点位带的消息信息  也可通过这个传输数据把数据转成json
        mSnippet = marker.snippet
        //当前点位带的标题信息
        mTitle = marker.title
    }

    override fun getInfoContents(p0: Marker?): View? {
        return null //因为是自定义的布局，返回null
    }

    init {
        mContext = context
        mData = data
    }

    @SuppressLint("MissingInflatedId", "SetTextI18n", "InflateParams")
    private fun initView(): View {
        //获取自定义的布局
        val view = LayoutInflater.from(mContext).inflate(R.layout.marker_view, null)
        tvWeather = view.findViewById(R.id.tv_weather)
        tvTemperature = view.findViewById(R.id.tv_temperature)
        tvWindDirectionPower = view.findViewById(R.id.tv_wind_direction_power)
        tvWeatherInfoFlyTime = view.findViewById(R.id.tv_weather_info_fly_time)
        tvWeatherInfoFlyPlace = view.findViewById(R.id.tv_weather_fly_place)
        tvWeatherInfoDistance = view.findViewById(R.id.tv_weather_info_distance)

        tvWeather.text = mContext!!.getString(R.string.Weather_Now) + " " + mData.weather
        tvTemperature.text = mData.temperature + "°"
        tvWindDirectionPower.text =
            mContext!!.getString(R.string.wind_direction) + ": " + mData.windDirection + mContext!!.getString(R.string.wind) + "  " + mData.windPower + mContext!!.getString(
                R.string.level
            )
        tvWeatherInfoFlyTime.text =
            if (mData.flyTime.isNotEmpty()) mContext!!.getString(R.string.item_train_fly_main_list_time) + " " + mData.flyTime else mContext!!.getString(
                R.string.time_unknown
            )
        tvWeatherInfoFlyPlace.text =
            if (mData.flyplace.isNotEmpty()) mContext!!.getString(R.string.address) + "：" + mData.flyplace else mContext!!.getString(R.string.address_unknown)
        tvWeatherInfoDistance.text =
            if (mData.flyDistance.isEmpty()) mContext!!.getString(R.string.Homing_point) else mData.flyDistance + mContext!!.getString(R.string.kilometre)
        return view
    }
}